package com.tomcat;

import javax.net.ServerSocketFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TomcatService {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        while (true){
            System.out.println("准备接收");
            Socket socket = serverSocket.accept();
            System.out.println(socket);
            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();
            String ass="HTTP/1.1 202 Accepted\n" +
                    "Date: Mon, 27 Jul 2009 12:28:53 GMT\n" +
                    "Server: Apache\n" ;

            outputStream.write(ass.getBytes());
            outputStream.flush();

            byte[] bytes = new byte[1024*8];
            int len=0;

            while (true){
                len=inputStream.read(bytes);
                System.out.println(new String(bytes,0,len));
                if (len<1024){
                    break;
                }

            }


            String body="<html><head></head><body><h3>helloworld</h3></body></html>";
            String string="HTTP/1.1 200 OK\n" +
//                    "Location: http://www.baidu.com\n" +
                    "Date: Mon, 27 Jul 2009 12:28:53 GMT\n" +
                    "Server: Apache\n" +
                    "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\n" +
                    "ETag: \"34aa387-d-1568eb00\"\n" +
                    "Accept-Ranges: bytes\n" +
                    "Content-Length: "+ body.length()+"\n" +
                    "Vary: Accept-Encoding\n" +
                    "Content-Type: text/html" +
                    "\n" +body;


            outputStream.write(string.getBytes());
            outputStream.flush();

            inputStream.close();
            outputStream.close();
        }


    }
}
